import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;

public class DAY0124 {
    public boolean isValid(String s) {
        int n = s.length();
        if (n % 2 == 1) {
            return false;
        }

        Map<Character, Character> pairs = new HashMap<Character, Character>() {{
            put(')', '(');
            put(']', '[');
            put('}', '{');
        }};
        Deque<Character> stack = new LinkedList<Character>();
        for (int i = 0; i < n; i++) {
            char ch = s.charAt(i);
            if (pairs.containsKey(ch)) {
                if (stack.isEmpty() || stack.peek() != pairs.get(ch)) {
                    return false;
                }
                stack.pop();
            } else {
                stack.push(ch);
            }
        }
        return stack.isEmpty();
    }




    class MyStack {
        private Queue<Integer>qu1;
        private Queue<Integer>qu2;
        public MyStack() {
            qu1=new LinkedList<>();
            qu2=new LinkedList<>();
        }

        public void push(int x) {
            if(!qu1.isEmpty()){
                qu1.offer(x);
            } else if(!qu2.isEmpty()){
                qu2.offer(x);
            }else {
                qu1.offer(x);
            }
        }

        public int pop() {
            //出栈
            if(empty()){
                return -1;
            }
            if(!qu1.isEmpty()){
                int size=qu1.size();
                for (int i = 0; i < size-1; i++) {
                    qu2.offer(qu1.poll());
                }
                return qu1.poll();
            }else{
                int size=qu2.size();
                for (int i = 0; i < size-1; i++) {
                    qu1.offer(qu2.poll());
                }
                return qu2.poll();
            }
        }

        public int top() {
            if(empty()){
                return -1;
            }
            if(!qu1.isEmpty()){
                int size=qu1.size();
                int ret=-1;
                for (int i = 0; i < size; i++) {
                    ret=qu1.poll();
                    qu2.offer(ret);
                }
                return ret;
            }else{
                int size=qu2.size();
                int ret=-1;
                for (int i = 0; i < size; i++) {
                    ret=qu2.poll();
                    qu1.offer(ret);
                }
                return ret;
            }
        }

        public boolean empty() {
            return qu1.isEmpty()&&qu2.isEmpty();
        }
    }

}
